package com.bob.debt.utils;


import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 时间工具类
 *
 * @author chao.wang
 */
public class DateUtil {

    private static final String DEFAULT_PATTERN = "yyyy-MM-dd";
    private static final String DEFAULT_FULL_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String date2String(LocalDateTime dateTime, String pattern) {
        if (null == dateTime) {
            dateTime = LocalDateTime.now();
        }
        if (StringUtils.isBlank(pattern)) {
            pattern = DEFAULT_PATTERN;
        }
        return dateTime.format(getDateTimeFormatter(pattern));
    }

    private static DateTimeFormatter getDateTimeFormatter(String pattern) {
        return DateTimeFormatter.ofPattern(pattern);
    }

}
